<?php
/**
 * 
 */

/**
 * 
 */

/**
 * Class to time the page load time
 *
 * @author anthony
 */
final class PageTimer {
    protected $time_start = 0;
    protected $time_end = 0;
    
    /**
     * Constructor for the timer class ( calls $this->start() ) 
     */
    public function __construct() {
	$this->start();
    }
    
    /**
     * Destructor for this class 
     */
    public function __destruct() {}
    
    /**
     * Start Method: Logs the current time as the start time, returns this timestamp
     * @return double 
     */
    public function start() {
	$this->time_start = $this->currenttime();
	return $this->time_start;
    }
    
    /**
     * Finish Method: Logs the current time as the finish time, returns this timestamp
     * @return double 
     */
    public function finish() {
	$this->time_end = $this->currenttime();
	return $this->time_end;
    }
    
    /**
     * Gets the current time ( time + microtime ) for a 0.00000 format timestamp
     * @return double 
     */
    protected function currenttime() {
	return (time()+microtime());
    }
    
    /**
     * Return a type double difference between start and finish in seconds, eg: 0.017
     * @return double 
     */
    public function getTime() {
	if($this->time_start !== 0 && $this->time_end === 0) {
	    $this->finish();
	}
	if($this->time_start === 0) {
	    return 0; // huh?
	} else {
	    return ($this->time_end - $this->time_start);
	}
    }
    
    /**
     * Return a integer type difference between start and finish in milliseconds, eg: 17
     * @return int 
     */
    public function getTimeMilli() {
	$val = $this->getTime();
	return ceil(($val * 1000));
    }
}
$PageTimer = new PageTimer();
?>
